<?php
/**
 * Created by PhpStorm.
 * User: zhangjian37
 * Date: 2016/9/25
 * Time: 13:44
 */
$RootDir = $_SERVER['DOCUMENT_ROOT'];
include $RootDir.'/utils/mysql/MysqlUtils.php';

include 'Juhuansuan.php';

function curl_file_get_contents($durl){
    $header = array('Expect:');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $durl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    $r = curl_exec($ch);
    curl_close($ch);
    return $r;
}


function flush_jhs_data()
{
    $dataArray = array('T' => 'Juhuasuan');

    $page = 1;
    $count = 0;
    $callback = "jsonp1";
    $urlPre = "https://ju.taobao.com/json/tg/ajaxGetItemsV2.json?page=";
    while (true) {
        if ($count == 0 && $page != 1) break;

        $durl = $urlPre . $page . "&psize=96&type=0&label=flsremind&scene=fls&stype=psort&rank=&jview=all&includeForecast=true&ostimeLower=1474703245289&_=1474703245289&callback=" . $callback;
        $content = curl_file_get_contents($durl);

        $content = str_replace($callback . "(", "", $content);
        $content = substr($content, 0, strlen($content) - 1);
        $jsonContent = json_decode($content);
        $items = $jsonContent->itemList;
        $count = count($items);
        for ($i = 0; $i < $count; $i++) {
            $juhuasuan = new Juhuasuan();
            $item = $items[$i];
            try {
                //name
                $juhuasuan->name = $item->name->shortName;
                //pic
                $juhuasuan->pic = $item->baseinfo->picUrlNew;
                //url
                ////ju.taobao.com/m/jusp/alone/detailwap/mtp.htm?ju_id='+e.juId+"&item_id="+e.itemId+"&flstask=1&flsdouble=false&includeForecast=false&_target=_blank
                $juId = $item->baseinfo->juId;
                $itemId = $item->baseinfo->itemId;
                $juhuasuan->itemid = $itemId;
                $juhuasuan->url = "//ju.taobao.com/m/jusp/alone/detailwap/mtp.htm?item_id=" . $itemId . "&45&flstask=1&includeForecast=true";
                //$actPrice
                $juhuasuan->actPrice = $item->price->actPrice;
                //$origPrice
                $juhuasuan->origPrice = $item->price->origPrice;

                //reminNum
                $juhuasuan->reminNum = $item->remind->remindNum;
                //hb
                $hb = $item->extend->flsHb;
                list($mixPrice, $maxPrice) = split("_", $hb);
                if($mixPrice > 0){$juhuasuan->minPrice = $mixPrice;}
                else{$juhuasuan->minPrice = 0.0;}
                if($maxPrice > 0){$juhuasuan->maxPrice = $maxPrice;}
                else{$juhuasuan->maxPrice = 0.0;}
                $dataArray[] = $juhuasuan;
            } catch (Exception $e) {
            }
        }
        echo "page=" . $page . ",count=" . $count . "<br>";
        if ($count < 96 && $page != 1) break;
        $page++;
    }

    print count($dataArray) . "<br>";
    $juhuasuan0 = $dataArray[0];
    echo $juhuasuan0->itemid . "," . $juhuasuan0->name . "," . $juhuasuan0->url . "," . $juhuasuan0->minPrice . "," . $juhuasuan0->maxPrice . "," . $juhuasuan0->pic . "," . $juhuasuan0->actPrice . "," . $juhuasuan0->origPrice."<br>";
    $dataNum = count($dataArray);
    $conn = conn_mysql("youhuikuaibao");

//    $query = "truncate table youhuikuaibao.juhuasuan ";
//    if (!mysql_query($query, $conn)) {
//        die('err: ' . mysql_error());
//    }

    for ($i = 0; $i < $dataNum; $i++) {
        $cout = -1;
        $item = $dataArray[$i];
        $itemid = $item->itemid;
        if ($itemid == null) continue;
        $query = "select count(1) as cout from juhuasuan where itemid = '$itemid' limit 1";
        #echo $query . "<br>";
        $result = queryRecord($query, $conn);
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $cout = $row['cout'];
            break;
        }
        $minPrice = isset($item->minPrice) ? 0 : $item->minPrice;
        $maxPrice = (isset($item->maxPrice) || empty($a)) ? 0 : $item->maxPrice;
        if ($cout == 1) {
            $query = "update juhuasuan set url='$item->url',name='$item->name',pic='$item->pic',people_num='$item->reminNum',
              min_price='$item->minPrice',max_price='$item->maxPrice',actPrice='$item->actPrice',origPrice='$item->origPrice' where itemid = '$itemid'";
        } else {
            $query = "insert into juhuasuan(itemid,url,name,pic,people_num,min_price,max_price,actPrice,origPrice) VALUES
               ('$itemid','$item->url','$item->name','$item->pic',$item->reminNum,$minPrice,$maxPrice,'$item->actPrice','$item->origPrice') ";
        }
        if (!mysql_query($query, $conn)) {
            echo $minPrice . "<br>";
            echo $maxPrice . "<br>";
            echo $query."<br>";
            die('err: ' . mysql_error());
        }
    }
}

ignore_user_abort(false);//当用户关闭页面时服务停止
set_time_limit(0);  //设置执行时间，单位是秒。0表示不限制。
date_default_timezone_set('Asia/Shanghai');

$loop = 0;
while(TRUE){
    //这里是需要定时执行的任务
    flush_jhs_data();
    echo "--".$loop."<br>";
    $loop += 1;
}

